﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace VWise_DAO
{
    public class CVWise_DAO_Access
    {
        CPersonalManager pPersonalManager = new CPersonalManager();
        CVehicleManager pVehicleManager = new CVehicleManager();

        //para consulta o registro de ingreso
        public int iPKCodeToConsult;
        public bool bRegister = false;
        public DateTime dtRegisterDateTime = DateTime.Now;
        public string szPuertaAcceso = "";
        public int idAutoVehicle = 0;

        public bool bThreadRunning = false;

        public delegate void VWise_DAO_QueryHandler(object pCWise_DAO_Access, CVWise_DAO_EventArgs eventInfo);
        public event VWise_DAO_QueryHandler OnVWise_DAO_Query;


        public void Configure()
        {
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVWise_DAO_Access->Configure");

            //lectura de configuracion
            pPersonalManager.Configure();
            pVehicleManager.Configure();
        }

        #region Logica Personal
        /// <summary>
        /// Inicia el Thread de consulta de personal
        /// </summary>
        public void StartQueryPersonalByIdentifier()
        {
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVWise_DAO_Access->StartQueryPersonalByIdentifier ");

            if (bThreadRunning)
            {
                CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVWise_DAO_Access->StartQueryPersonalByIdentifier Thread Running");
                return;
            }

            Thread threadQuery = new Thread(new ThreadStart(QueryPersonalByIdentifier));
            threadQuery.Start();
            bThreadRunning = true;
        }


        /// <summary>
        /// funcion de consulta de personal
        /// </summary>
        public void QueryPersonalByIdentifier()
        {
            CPersonal pPersonal = null;
            CVWise_DAO_EventArgs pEventArgs = null;

            CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "*****************************************************");
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->QueryPersonalByIdentifier: " + iPKCodeToConsult.ToString());

            pPersonal = pPersonalManager.GetPersonalByID(iPKCodeToConsult);

            if (pPersonal != null)
            {
                //si existe la persona
                pEventArgs = new CVWise_DAO_EventArgs(pPersonal);

                if (bRegister)
                {
                    pEventArgs.iTypeMark = RegisterPersonalInOut(pPersonal);
                    pEventArgs.dtInOutMark = dtRegisterDateTime;
                }

                if (OnVWise_DAO_Query != null)
                {
                    CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->QueryPersonalByIdentifier: " +
                                            pPersonal.szPatLastName + " " +
                                            pPersonal.szMatlastName + " " +
                                            pPersonal.szFirstName);
                    OnVWise_DAO_Query(this, pEventArgs);
                }
                bThreadRunning = false;
                return;
            }

            pEventArgs = new CVWise_DAO_EventArgs();
            if (OnVWise_DAO_Query != null)
            {
                CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->QueryPersonalByIdentifier: NOT FOUND");
                OnVWise_DAO_Query(this, pEventArgs);
            }

            bThreadRunning = false;
            bRegister = false;
        }

        /// <summary>
        /// funcion de registro de marcas
        /// </summary>
        /// <param name="pPersonal"></param>
        /// <returns></returns>
        public int RegisterPersonalInOut(CPersonal pPersonal)
        {
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->RegisterPersonalInOut: " + pPersonal.iPKCode.ToString());
            return pPersonalManager.RegisterPersonalMark(pPersonal, dtRegisterDateTime, szPuertaAcceso, idAutoVehicle);
        }

        public List<CPersonal> SearchForPersonal(string szName, string szPatLastName, string szMatLastName, string szArea)
        {
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->SearchForPersonal: " + szName + "," +
                                                                                szPatLastName + "," +
                                                                                szMatLastName + "," +
                                                                                szArea);

            return pPersonalManager.SearchForPersonal(szName, szPatLastName, szMatLastName, szArea);
        }

        public List<CMarkRegister> SearchMarks(int iSelection, int iPKCode, string szStartDate, string szEndDate)
        {
            List<CMarkRegister> lListMarks = null;

            CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVWise_DAO_Access->SearchMarks:" + iPKCode.ToString() + "," +
                                                                          szStartDate + "," +
                                                                          szEndDate);

            if (iSelection == CMarkRegister.VWISE_DHL_PERSONAL)
                lListMarks = pPersonalManager.SearchMarks(iPKCode, szStartDate, szEndDate);
            else
                lListMarks = pVehicleManager.SearchMarks(iPKCode, szStartDate, szEndDate);

            return lListMarks;

        }

        public List<CConductor> GetAllConductores()
        {

            CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVWise_DAO_Access->GetAllConductores");

            return pPersonalManager.GetAllConductor();
        }




        #endregion

        #region Logica Vehiculos

        /// <summary>
        /// Inicia el Thread de consulta de vehiculos
        /// </summary>
        public void StartQueryVehicleByIdentifier()
        {
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVWise_DAO_Access->StartQueryVehicleByIdentifier ");

            if (bThreadRunning)
            {
                CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVWise_DAO_Access->StartQueryVehicleByIdentifier Thread running");
                return;
            }

            Thread threadQuery = new Thread(new ThreadStart(QueryVehicleByIdentifier));
            threadQuery.Start();

            bThreadRunning = true;
        }

        /// <summary>
        /// funcion de consulta de vehiculos
        /// </summary>
        public void QueryVehicleByIdentifier()
        {
            CVehicle pVehicle = null;
            CVWise_DAO_EventArgs pEventArgs = null;

            CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "*****************************************************");
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->QueryVehicleByIdentifier: " + iPKCodeToConsult.ToString());

            pVehicle = pVehicleManager.GetVehicleByID(iPKCodeToConsult);

            if (pVehicle != null)
            {
                pEventArgs = new CVWise_DAO_EventArgs(pVehicle);

                if (bRegister)
                {
                    pEventArgs.iTypeMark = RegisterVehicleInOut(pVehicle);
                    pEventArgs.dtInOutMark = dtRegisterDateTime;
                }

                if (OnVWise_DAO_Query != null)
                {
                    CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->QueryVehicleByIdentifier: " +
                                            pVehicle.szPlaca + " " +
                                            pVehicle.szMarca + " " +
                                            pVehicle.szModelo);
                    OnVWise_DAO_Query(this, pEventArgs);
                }
                bThreadRunning = false;
                return;
            }

            pEventArgs = new CVWise_DAO_EventArgs();

            if (OnVWise_DAO_Query != null)
            {
                CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->QueryVehicleByIdentifier: NOT FOUND");
                OnVWise_DAO_Query(this, pEventArgs);
            }

            bThreadRunning = false;
            bRegister = false;
            szPuertaAcceso = "";
        }

        /// <summary>
        /// funcion de registro de marcas
        /// </summary>
        /// <param name="pVehicle"></param>
        /// <returns></returns>
        public int RegisterVehicleInOut(CVehicle pVehicle)
        {
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->RegisterVehicleInOut: " + pVehicle.iPKCode.ToString());
            return pVehicleManager.RegisterVehicleMark(pVehicle, dtRegisterDateTime, szPuertaAcceso);
        }

        public List<CVehicle> SearchForVehicle(string szName, string szPatLastName, string szMatLastName,
                                              string szPlaca, string szMarca, string szModelo)
        {
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->SearchForVehicle: " + szName + "," +
                                                                          szPatLastName + "," +
                                                                          szMatLastName + "," +
                                                                          szPlaca + "," +
                                                                          szMarca + "," +
                                                                          szModelo);

            return pVehicleManager.SearchForVehicle(szName, szPatLastName, szMatLastName,
                                                 szPlaca, szMarca, szModelo);
        }

        public int CreateNewVehicle(CVehicle pVehicle)
        {
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->CreateNewVehicle");

            return pVehicleManager.CreateNewVehicle(pVehicle);
        }

        public List<CVehicle> GetAllVehicles()
        {
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->GetAllVehicles");

            return pVehicleManager.GetAllVehicles();
        }

        public int DeleteVehicle(CVehicle pVehicle)
        {
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->DeleteVehicle");

            return pVehicleManager.DeleteVehicle(pVehicle);

        }

        public int UpdateVehicle(CVehicle pVehicle)
        {
            CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVWise_DAO_Access->UpdateVehicle");

            return pVehicleManager.UpdateVehicle(pVehicle);

        }


        #endregion
    }
}
